package com.cfl.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * API
 * 
 * @author chenfenli
 * 
 */
@SuppressWarnings("serial")
public class AppResponse implements Serializable {

	private static Integer pageSize = 10;
	
	private String code;
	private String msg;
	private Integer pageNumber;
	private Boolean isLastPage;
	private Object object;

	public AppResponse()
	{
		this.code = "";
		this.msg = "成功";
		this.isLastPage = false;
	}
	public AppResponse(Object object) {
		this.code = "0";
		this.msg = "成功";
		objectPrameasToString(object);
		this.object = object;
		this.isLastPage = false;
	}
	
	public void objectPrameasToString(Object object)
	{
		try {
			//得到类对象  
		    @SuppressWarnings("rawtypes")
			Class obj = (Class) object.getClass();
		    
		    // 得到类中的所有属性集合 
		    Field[] fs = obj.getDeclaredFields();  
		       for(int i = 0 ; i < fs.length; i++){  
		           Field f = fs[i];  
		           f.setAccessible(true); //设置些属性是可以访问的  
		           Object val = f.get(object);//得到此属性的值   
		           
		           String type = f.getType().toString();//得到此属性的类型  
		           if (type.endsWith("String")) {  
		              if(StringUtils.isEmpty((String)val))
		              {
		            	  f.set(object,"") ; 
		              }
		           }else if(type.endsWith("int") || type.endsWith("Integer")){  

		           }  
		       } 
		} catch (Exception e) {
			// TODO: handle exception
		} 
	}

	public AppResponse(String code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public AppResponse error(String code,String msg) {
		this.code = code;
		this.msg = msg;
		this.isLastPage = false;
		return this;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}
	
	public Object getObject() {
		return object;
	}
	public void setObject(Object object) {
		this.object = object;
	}

	public Boolean getIsLastPage() {
		return isLastPage;
	}

	public void setIsLastPage(Boolean isLastPage) {
		this.isLastPage = isLastPage;
	}
	
	public static int getPageSize()
	{
		return AppResponse.pageSize;
	}
}
